package deviceConfig

import (
	"context"
	"devops-super/api/configs/v1"
	"devops-super/internal/model/entity"
	"devops-super/internal/service"
	"github.com/gogf/gf/v2/os/gtime"
)

// GetPageLst 分页获取设备配置列表
func (*ControllerV1) GetPageLst(ctx context.Context, req *v1.DeviceConfigGetPageLstReq) (res *v1.DeviceConfigGetPageLstRes, err error) {
	res = &v1.DeviceConfigGetPageLstRes{}
	res.PageLstRes, err = service.DeviceConfig().GetPageLst(ctx, req.PageLstReq)
	return
}

// Add 新增设备配置
func (*ControllerV1) Add(ctx context.Context, req *v1.DeviceConfigAddReq) (res *v1.DeviceConfigAddRes, err error) {
	config := &entity.AppDeviceConfig{
		DeviceNo:    req.DeviceNo,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		CreatedAt:   gtime.Now(),
	}
	err = service.DeviceConfig().Add(ctx, config)
	return
}

// Upt 更新设备配置
func (*ControllerV1) Upt(ctx context.Context, req *v1.DeviceConfigUptReq) (res *v1.DeviceConfigUptRes, err error) {
	config := &entity.AppDeviceConfig{
		Id:          int64(req.Id),
		DeviceNo:    req.DeviceNo,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		UpdatedAt:   gtime.Now(),
	}
	err = service.DeviceConfig().Upt(ctx, config)
	return
}

// Del 删除设备配置
func (*ControllerV1) Del(ctx context.Context, req *v1.DeviceConfigDelReq) (res *v1.DeviceConfigDelRes, err error) {
	err = service.DeviceConfig().Del(ctx, req.Id)
	return
}

// GetOne 获取单条设备配置
func (*ControllerV1) GetOne(ctx context.Context, req *v1.DeviceConfigGetOneReq) (res *v1.DeviceConfigGetOneRes, err error) {
	res1, err := service.DeviceConfig().GetOne(ctx, req.Id)
	if err != nil {
		return
	}
	res = new(v1.DeviceConfigGetOneRes)
	res.Info = res1
	return
}

// GetConfig 获取设备配置
func (*ControllerV1) GetConfig(ctx context.Context, req *v1.GetDeviceConfigReq) (res *v1.GetDeviceConfigRes, err error) {
	res, err = service.DeviceConfig().GetConfig(ctx, req)
	return
}

// SetConfig 保存设备配置
func (*ControllerV1) SetConfig(ctx context.Context, req *v1.SetDeviceConfigReq) (res *v1.SetDeviceConfigRes, err error) {
	res = &v1.SetDeviceConfigRes{}
	res, err = service.DeviceConfig().SetConfig(ctx, req)
	return
}
